import 'package:shared_preferences/shared_preferences.dart';

/// 缓存管理类
class HiCache {
  HiCache._() {
    init();
  }

  late SharedPreferences sharedPreferences;

  static HiCache _instance = HiCache._();

  static HiCache getInstance() {
    return _instance;
  }

  HiCache._pre(SharedPreferences prefs) {
    sharedPreferences = prefs;
  }

  static Future<HiCache> preInit() async {
    var prefs = await SharedPreferences.getInstance();
    _instance = HiCache._pre(prefs);
    return _instance;
  }

  void init() async {
    if (sharedPreferences == null) {
      sharedPreferences = await SharedPreferences.getInstance();
    }
  }

  setString(String key, String value) {
    sharedPreferences.setString(key, value);
  }

  setDouble(String key, double value) {
    sharedPreferences.setDouble(key, value);
  }

  setInt(String key, int value) {
    sharedPreferences.setInt(key, value);
  }

  setBool(String key, bool value) {
    sharedPreferences.setBool(key, value);
  }

  setStringList(String key, List<String> value) {
    sharedPreferences.setStringList(key, value);
  }

  T get<T>(String key) {
    return sharedPreferences.get(key);
  }
}
